package com.wuye.basic.controller;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.wuye.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wuye.common.annotation.Log;
import com.wuye.common.core.controller.BaseController;
import com.wuye.common.core.domain.AjaxResult;
import com.wuye.common.enums.BusinessType;
import com.wuye.basic.domain.WyCommunity;
import com.wuye.basic.service.IWyCommunityService;
import com.wuye.common.utils.poi.ExcelUtil;
import com.wuye.common.core.page.TableDataInfo;

/**
 * 小区Controller
 * 
 * @author avalon
 * @date 2024-11-23
 */
@RestController
@RequestMapping("/basic/community")
@Slf4j
public class WyCommunityController extends BaseController
{
    @Autowired
    private IWyCommunityService wyCommunityService;

    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('basic:community:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyCommunity wyCommunity)
    {
        startPage();
//        List<WyCommunity> list = wyCommunityService.selectWyCommunityList(wyCommunity);
        List<WyCommunity> list = wyCommunityService.listByInfo(wyCommunity);
        return getDataTable(list);
    }

    /**
     * 导出小区列表
     */
    @PreAuthorize("@ss.hasPermi('basic:community:export')")
    @Log(title = "小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyCommunity wyCommunity)
    {
        List<WyCommunity> list = wyCommunityService.selectWyCommunityList(wyCommunity);
        ExcelUtil<WyCommunity> util = new ExcelUtil<WyCommunity>(WyCommunity.class);
        util.exportExcel(response, list, "小区数据");
    }

    /**
     * 获取小区详细信息
     */
    @PreAuthorize("@ss.hasPermi('basic:community:query')")
    @GetMapping(value = "/{communityId}")
    public AjaxResult getInfo(@PathVariable("communityId") Long communityId)
    {
        return success(wyCommunityService.getById(communityId));
    }

    /**
     * 新增小区
     */
    @PreAuthorize("@ss.hasPermi('basic:community:add')")
    @Log(title = "小区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyCommunity wyCommunity)
    {
        wyCommunity.setCreateTime(DateUtils.getNowDate());
        return toAjax(wyCommunityService.save(wyCommunity));
    }

    /**
     * 修改小区
     */
    @PreAuthorize("@ss.hasPermi('basic:community:edit')")
    @Log(title = "小区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyCommunity wyCommunity)
    {
        return toAjax(wyCommunityService.updateById(wyCommunity));
    }

    /**
     * 删除小区
     */
    @PreAuthorize("@ss.hasPermi('basic:community:remove')")
    @Log(title = "小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{communityIds}")
    public AjaxResult remove(@PathVariable Long[] communityIds)
    {
        log.info("删除小区");
        List<Long> ids = Arrays.asList(communityIds);
        return toAjax(wyCommunityService.removeBatchByIds(ids));
    }


    /**
     * 查询小区列表
     */
    @PreAuthorize("@ss.hasPermi('basic:community:listToName')")
    @GetMapping("/listToName")
    public AjaxResult listToName()
    {
      return AjaxResult.success(wyCommunityService.listToName());
    }


}
